<?php  
if ($_GET['data_inicio']):
    $data_inicio=$_GET['data_inicio'];
else:
    $data_inicio="2008-07-01 00:00:00";
endif;
if ($_GET['data_fim']):
    $data_fim=$_GET['data_fim'];
else:
    $data_fim="2008-07-31 23:59:59";
endif;

$connection = new mysqli("localhost","gps","gpsDB","gps");
$query = "select latitude,longitude,altitude,course,speed,time from points where speed<100000 and altitude<100000 and time>=".date('U', strtotime($data_inicio))." and time<=".date('U', strtotime($data_fim))." order by time";
$result = $connection->query($query);

header("Content-type: text/xml"); 
$dom = new DOMDocument("1.0", "UTF-8");

$node = $dom->appendChild($dom->createElementNS('http://www.topografix.com/GPX/1/1','gpx'));
$node->setAttribute('creator', 'Wagner Sartori Junior');
$node->setAttribute('version', '1.1');
$node->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$node->setAttribute('xsi:schemaLocation', 'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd');

$metadatanode = $dom->appendChild($node);
$metadata = $dom->createElement("metadata");
$bounds = $dom->createElement("bounds");
$bounds->setAttribute("minlat", $connection->query("select min(latitude) as 'minlat' from points where speed<100000 and altitude<100000 and time>=".date('U', strtotime($data_inicio))." and time<=".date('U', strtotime($data_fim)))->fetch_object()->minlat);
$bounds->setAttribute("minlon", $connection->query("select min(longitude) as 'minlon' from points where speed<100000 and altitude<100000 and time>=".date('U', strtotime($data_inicio))." and time<=".date('U', strtotime($data_fim)))->fetch_object()->minlon);
$bounds->setAttribute("maxlat", $connection->query("select max(latitude) as 'maxlat' from points where speed<100000 and altitude<100000 and time>=".date('U', strtotime($data_inicio))." and time<=".date('U', strtotime($data_fim)))->fetch_object()->maxlat);
$bounds->setAttribute("maxlon", $connection->query("select max(longitude) as 'maxlon' from points where speed<100000 and altitude<100000 and time>=".date('U', strtotime($data_inicio))." and time<=".date('U', strtotime($data_fim)))->fetch_object()->maxlon);
$metadatanode->appendChild($metadata)->appendChild($bounds);

$ultimahora=0;
$printed=false;
$parnode = $dom->appendChild($node);
while ($row = $result->fetch_object()):
  if (($row->time-$ultimahora) > 300):
    $trk = $dom->createElement("trk");
    $name = $dom->createElement("name","Rota ".date('Y-m-d H:i:s', $row->time)."");
    $trkseg = $dom->createElement("trkseg");
    $trknode = $parnode->appendChild($trk);
    $namenode = $trknode->appendChild($name);
    $trksegnode = $trknode->appendChild($trkseg);
  endif;
  $ultimahora=$row->time;
  $trkpt = $dom->createElement("trkpt");
  $trkptnode = $trksegnode->appendChild($trkpt);
  $trkptnode->setAttribute("lat", $row->latitude);
  $trkptnode->setAttribute("lon", $row->longitude);
  $ele = $dom->createElement("ele", $row->altitude);
  $trkptnode->appendChild($ele);
  $speed = $dom->createElement("speed", $row->speed*3.6); //Convert from m/s to km/h
  $trkptnode->appendChild($speed);
  $course = $dom->createElement("course", $row->course);
  $trkptnode->appendChild($course);
  $time = $dom->createElement("time", date('c', $row->time));
  $trkptnode->appendChild($time);
endwhile;
 
echo $dom->saveXML();
?>
